import pytest
from selenium.webdriver.common.by import By
import time
from common.driverrr import init_webdriver
from common.shibie import crop_captcha
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


@pytest.fixture(scope="function")
def setup():
    print("测试开始前执行")
    yield
    print("测试结束后执行")


@pytest.fixture(scope="function")
def setupui():
    # 测试前：打开浏览器并访问特定页面
    print("\n启动浏览器...")
    # options = webdriver.ChromeOptions()
    # # options.add_argument( "--headless" )  # 无头模式，不显示浏览器窗口
    # options.add_argument( "--disable-gpu" )  # 禁用 GPU 加速
    # options.add_argument( "--no-sandbox" )  # 禁用沙盒模式
    # options.add_argument( "--disable-dev-shm-usage" )  # 解决资源限制问题
    # # 使用 webdriver_manager 自动管理 ChromeDriver
    # # driver = webdriver.Firefox( service=Service( ChromeDriverManager().install() ), options=options )
    # # 不好用，加载不出来
    # services = Service(r'C:\Program Files\Google\Chrome\Application\chromedriver.exe')
    # driver = webdriver.Chrome(service=services, options=options)

    # 方式1：手动指定 chromedriver 路径
    driver = init_webdriver(driver_path=r"C:\Program Files\Google\Chrome\Application\chromedriver.exe")
    # 方式2：自动下载 chromedriver（需安装 webdriver-manager）
    # driver = init_webdriver(headless=True)  # 无头模式

    driver.maximize_window()  # 最大化窗口

    # 访问特定页面
    driver.get("https://espace.sipedu.cn/portalweb/#/home/page?type=1&id=1470278959297478657")  # 替换为你的目标页面
    time.sleep(3)
    driver.find_element(By.XPATH,
                        '/html/body/div/div/section/main/div/div[1]/div[2]/div[2]/form/div[3]/div/div/input').send_keys(
        'yjw')
    driver.find_element(By.CSS_SELECTOR,
                        '#app > div > section > main > div > div.login_page_wrap > div.right_div > div.right_div_lftab > form > div:nth-child(4) > div > div > input').send_keys(
        '123456jw')
    driver.find_element(By.XPATH,
                        '/html/body/div/div/section/main/div/div[1]/div[2]/div[2]/form/div[5]/div[1]/div/div/div/div[2]').click()
    time.sleep(1)
    # # 点击刷新验证码
    # driver.find_element(By.CLASS_NAME, "captcha-img").click()
    # # 保存整个页面截图:ml-citation{ref="6" data="citationList"}
    # driver.save_screenshot("./temp/page.png")
    # captcha_element = driver.find_element(By.CLASS_NAME, "captcha-img")
    # location = captcha_element.location  # 获取验证码坐标:ml-citation{ref="6" data="citationList"}
    # size = captcha_element.size  # 获取验证码尺寸:ml-citation{ref="6" data="citationList"}
    # # 调用shibie中的函数识别验证码
    # captcha_text = crop_captcha("./temp/page.png", location, size)
    # driver.find_element(By.XPATH,
    #                     '//*[@id="app"]/div/section/main/div/div[1]/div[2]/div[2]/form/div[6]/div/div/div/input').send_keys(captcha_text)
    # time.sleep(2)
    # # 点击登 录
    # driver.find_element(By.XPATH,
    #                     '//*[@id="app"]/div/section/main/div/div[1]/div[2]/div[2]/form/div[7]/div/button').click()
    # time.sleep(3)

    # 获取当前页面url
    if driver.current_url != "https://espace.sipedu.cn/portalweb/#/selfWork":
        while True:
            driver.find_element(By.CLASS_NAME, "captcha-img").click()
            WebDriverWait(driver, 10).until(
                EC.presence_of_element_located((By.CLASS_NAME, "captcha-img"))
            )
            driver.save_screenshot("./temp/page.png")  # 保存整个页面截图:ml-citation{ref="6" data="citationList"}
            captcha_element = driver.find_element(By.CLASS_NAME, "captcha-img")
            location = captcha_element.location  # 获取验证码坐标:ml-citation{ref="6" data="citationList"}
            size = captcha_element.size  # 获取验证码尺寸:ml-citation{ref="6" data="citationList"}
            # 调用shibie中的函数识别验证码
            captcha_text = crop_captcha("./temp/page.png", location, size)

            driver.find_element(By.XPATH,
                                '//*[@id="app"]/div/section/main/div/div[1]/div[2]/div[2]/form/div[6]/div/div/div/input').send_keys(
                captcha_text)
            time.sleep(2)
            # 登 录
            driver.find_element(By.XPATH,
                                '//*[@id="app"]/div/section/main/div/div[1]/div[2]/div[2]/form/div[7]/div/button').click()
            time.sleep(2)
            if driver.current_url == "https://espace.sipedu.cn/portalweb/#/selfWork":
                break

    yield driver  # 将 driver 对象传递给测试函数
    # 测试后：关闭浏览器
    print("\n关闭浏览器...")
    driver.quit()
